﻿using System;
using System.Data;
using System.Linq;
using System.Net.Mail;

namespace WebTimer.Common
{
    public class Func
    {
        #region 计划任务

        /// <summary>
        /// 设置本次任务执行时间
        /// </summary>
        /// <param name="key"></param>
        /// <param name="serverName"></param>
        /// <param name="lastExecuted"></param>
        public void SetLastExecuteScheduledEventDateTime(string key, string serverName, DateTime lastExecuted)
        {
            
        }


        /// <summary>
        /// 获取最后一次任务执行时间
        /// </summary>
        /// <param name="key"></param>
        /// <param name="serverName"></param>
        /// <returns></returns>
        public DateTime GetLastExecuteScheduledEventDateTime(string key, string serverName)
        {
            var dir = string.Format("{0}log/", AppDomain.CurrentDomain.BaseDirectory);
            DataTable dt= WebTimer.Common.FileOperate.getDirectoryInfos(dir, FileOperate.FsoMethod.File);
            foreach (DataRow dr in dt.Rows)
            {
                string sss = dr["name"].ToString();
                if (key + ".log" == dr["name"].ToString())
                {
                    return Convert.ToDateTime(dr["lastWriteTime"]);
                }

            }
            return DateTime.Now;
        }
        #endregion


        /// <summary>
        /// 解析模板
        /// </summary>
        /// <param name="file"></param>
        /// <param name="context"></param>
        /// <returns></returns>
        public static string AnalysisMailTemp(string file, System.Collections.IDictionary context)
        {
            var content = FileOperate.ReadFile(string.Format("{0}mail/{1}", AppDomain.CurrentDomain.BaseDirectory, file));
            foreach(System.Collections.DictionaryEntry it in context)
            {
                content= content.Replace(it.Key.ToString(), it.Value.ToString());   
            }
            return content;
            
        }


        /// <summary>
        /// 验证输入框代码块
        /// </summary>
        public static bool validate(string str)
        {
            string strobj;
            bool flag = false;
            for (int i = 0; i < str.Length; i++)
            {
                strobj = str.Substring(i, 1);
                if (strobj == "%" || strobj == "&" || strobj == "'" || strobj == "|" || strobj == "<" || strobj == ">")
                {
                    flag = true;
                    break;
                }
            }
            return flag;
        }
    }
}
